﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using SERP.StringMappings;
using SERP.DAO;
using SERP.Controllers;
namespace SERP.Modules.People
{
    public partial class PeopleList : SERP.Modules.ModuleBase
    {
        private PeopleController controller = new PeopleController();
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadLanguage();
            if (!this.IsPostBack)
            {
                tv.LoadData();
                LoadData();
            }
        }
        private int GetPageIndex()
        {
            int pageIndex = 0;
            if (Request["PageIndex"] != null)
            {
                try
                {
                    pageIndex = int.Parse(Request["PageIndex"]);
                }
                catch
                {
                }
            }
            return pageIndex;
        }
        private string GetFilter()
        {
            string strFilter = "";
            if (txtPersonCode.Text.Trim() != "")
            {
                strFilter += " AND PersonCode LIKE '%" + txtPersonCode.Text.Trim() + "%'";
            }
            if (txtFullName.Text.Trim() != "")
            {
                strFilter += " AND FullName LIKE '%" + txtFullName.Text.Trim() + "%'";
            }
            if (txtGender.SelectedIndex>0)
            {
                strFilter += " AND Gender=" + txtGender.SelectedValue.Trim();
            }
            if (cbkFindBirthDay.Checked)
            {
                strFilter += " AND BirthDay BETWEEN " + txtBirthDayFROM.SelectedDate.Value.ToString() + " AND " + txtBirthDayTO.SelectedDate.Value.ToString();
            }
            if (txtAddress.Text.Trim() != "")
            {
                strFilter += " AND Address LIKE '%" + txtAddress.Text.Trim() + "%'";
            }
            if (txtPhone.Text.Trim() != "")
            {
                strFilter += " AND Phone LIKE '%" + txtPhone.Text.Trim() + "%'";
            }
            if (txtEmail.Text.Trim() != "")
            {
                strFilter += " AND Email LIKE '%" + txtEmail.Text.Trim() + "%'";
            }
            if (txtPositionId.SelectedIndex>0)
            {
                strFilter += " AND PositionId=" + txtPositionId.SelectedValue.Trim();
            }
            if (cbkFindStartDate.Checked)
            {
                strFilter += " AND StartDate BETWEEN " + txtStartDateFROM.SelectedDate.Value.ToString() + " AND " + txtStartDateTO.SelectedDate.Value.ToString();
            }
            if (txtNote.Text.Trim() != "")
            {
                strFilter += " AND Note LIKE '%" + txtNote.Text.Trim() + "%'";
            }
            if (txtOrganizationId.SelectedIndex > 0)
            {
                strFilter += " AND OrganizationId ='" + txtOrganizationId.SelectedValue + "'";
            }
            if (txtPersonTypeId.SelectedIndex > 0)
            {
                strFilter += " AND PersonTypeId ='" + txtPersonTypeId.SelectedValue + "'";
            }
            if (strFilter.Length > 0)
                strFilter = strFilter.Substring(4);
            return strFilter;
        }
        //Load All lable text
        private void LoadLanguage()
        {
            labelPersonCode.Text = PeopleStringMapping.PersonCode + ":";
            labelFullName.Text = PeopleStringMapping.FullName + ":";
            labelGender.Text = PeopleStringMapping.Gender + ":";            
            labelBirthDay.Text = PeopleStringMapping.BirthDay + ":";
            cbkFindBirthDay.Text = Constants.APP_SEARCH;
            labelAddress.Text = PeopleStringMapping.Address + ":";
            labelPhone.Text = PeopleStringMapping.Phone + ":";
            labelEmail.Text = PeopleStringMapping.Email + ":";
            labelPositionId.Text = PeopleStringMapping.PositionId + ":";
            labelStartDate.Text = PeopleStringMapping.StartDate + ":";
            cbkFindStartDate.Text = Constants.APP_SEARCH;
            labelNote.Text = PeopleStringMapping.Note + ":";
            labelOrganizationId.Text = PeopleStringMapping.OrganizationId + ":";
            labelPersonTypeId.Text = PeopleStringMapping.PersonTypeId + ":";
            btnSearch.Text = Constants.APP_SEARCH;
            btnAddNew.Text = Constants.APP_ADDNEW;
            btnDelete.Text = Constants.APP_DELETE;
            btnHelps.Text = Constants.APP_HELPS;
            btnImport.Text = Constants.APP_IMPORT;
            btnDelete.OnClientClick = "return confirm(\"" + Constants.APP_CONFIRM_DELELE + "\")";
            lblTitle.InnerText = Constants.APP_LIST + " " + PeopleStringMapping.People;
        }

        private void LoadData(bool clearCache)
        {
            //Load data to grid
            string filter = GetFilter();
            controller.LoadAllDSPeople(tv.GetSelectedAndChildId());
            controller.Table.DefaultView.RowFilter = filter;
            gridData.DataSource = controller.Table;
            //gridData.PageIndex=GetPageIndex();
            gridData.DataBind();
        }
        private void LoadData()
        {
            LoadData(true);
        }
        private Dictionary<string, string> GetParameter(string stParam)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (string st in stParam.Split(','))
            {
                string[] arr = st.Split('=');
                dic.Add(arr[0], arr[1]);
            }
            return dic;
        }
        private void DeleteData()
        {
            for (int indexRow = 0; indexRow < gridData.Items.Count; indexRow++)
            {
                RepeaterItem row = gridData.Items[indexRow];
                CheckBox cbkRow = (CheckBox)row.FindControl("cbkPeople");
                if (cbkRow != null)
                {
                    if (cbkRow.Checked)
                    {
                        LinkButton lkDelete = (LinkButton)row.FindControl("btnXoa");
                        Dictionary<string, string> dic = GetParameter(lkDelete.CommandArgument.ToString().Substring(1));
                        controller.DeleteByID(Int32.Parse(dic["PersonId"]));
                    }
                }
            }
        }
        protected void tv_SelectedNodeChanged(object sender, EventArgs e)
        {
            LoadData(false);
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData(false);
        }
        protected void OnAddNew(object sender, EventArgs e)
        {
            RedirectToPage("PeopleAddNew");
        }
        protected void OnImport(object sender, EventArgs e)
        {
            RedirectToPage("PeopleImport");
        }
        protected void OnDelete(object sender, EventArgs e)
        {
            DeleteData();
            LoadData();
        }
        protected void btnXem_OnCommand(object sender, CommandEventArgs e)
        {
            RedirectToPage("", "", "PeopleView", e.CommandArgument.ToString().Trim().TrimStart('&'));
        }
        protected void btnSua_OnCommand(object sender, CommandEventArgs e)
        {
            RedirectToPage("", "", "PeopleEdit", e.CommandArgument.ToString().Trim().TrimStart('&'));
        }
        protected void btnXoa_OnCommand(object sender, CommandEventArgs e)
        {
            RedirectToPage("", "", "PeopleDelete", e.CommandArgument.ToString().Trim().TrimStart('&'));
        }
    }
}
